// Create shortcut aliases for Common YUI utilities
var YDom = YAHOO.util.Dom;
var YEvent = YAHOO.util.Event;
var YDDM = YAHOO.util.DragDropMgr;


function displayMonthDayYear() {
	  var now = new Date();
	  var today = now.getDate();
	  var month = now.getMonth();
	  var monthName = new Array(12);
	      monthName[0]="JAN ";
	      monthName[1]="FEB ";
	      monthName[2]="MAR ";
	      monthName[3]="APR ";
	      monthName[4]="MAY ";
	      monthName[5]="JUNE ";
	      monthName[6]="JULY ";
	      monthName[7]="AUG ";
	      monthName[8]="SEPT ";
	      monthName[9]="OCT ";
	      monthName[10]="NOV ";
	      monthName[11]="DEC ";
	  var year = now.getFullYear();
	  var monthDayYear = monthName[month]+today+ ", "+year;
	  
	  document.write(monthDayYear);
	}


function onloadSelectNavItem(pageTitle){
	
	if (pageTitle.length > 0){

		var blnSetNavSelected;
		var navItem;
		switch(pageTitle)
		{
			case 'Home':
				navItem = document.getElementById("home");
				blnSetNavSelected = true;
			  break;
			case 'About Us':
				navItem = document.getElementById("aboutUs");
				blnSetNavSelected = true;
			  break;		
			case 'Menu':
				navItem = document.getElementById("menus");
				blnSetNavSelected = true;
				  break;
			case 'Photo Gallery':
				navItem = document.getElementById("photoGallery");
				blnSetNavSelected = true;
				  break;
			case 'Offer':
				navItem = document.getElementById("offer");
				blnSetNavSelected = true;
				  break;
			case 'Reviews':
				navItem = document.getElementById("reviews");
				blnSetNavSelected = true;
				  break;
			case 'Contact Us':
				navItem = document.getElementById("contactUs");
				blnSetNavSelected = true;
				  break;
			default:
				blnSetNavSelected = false;		  
			}	
		
		if(blnSetNavSelected == true){
			navItem.className = "current";			
		}
	}
}

function changeToUppercase(obj){
	eleById(obj.id).value=eleById(obj.id).value.toUpperCase();
}

function eleById(elementId){
	return document.getElementById(elementId);
}

/**
 * Clicks the button specified by the target param passed into this function if the enter key on the 
 * keyboard was pressed in any dom element contained by the dom element whose onkeypress event is attached
 * to this function.
 *  
 * @param event		The event from the onkeypress event of the element this is attached to
 * @param target		The id of the element to invoke the click() method on if the enter key was pressed
 * 
 * @return			Boolean			
 */
function formFireDefaultButton(event, target){
    var key;
    
    if (window.event){ 
    	key = window.event.keyCode; // IE
    } else {
    	key = event.which; //FF
    }
    if(key == 13){
	    var src = event.srcElement || event.target;
	    if(!src || (src.tagName.toLowerCase() != "textarea")){
            var defaultButton;
            defaultButton = document.getElementById(target);
            if (defaultButton && typeof(defaultButton.click) != "undefined"){
            	defaultButton.click();
                event.cancelBubble = true;
                if (event.stopPropagation) {
                	event.stopPropagation();
                }
                return false;
            }
	    }              
    }
    return true;
}
var isResponseJson = function(response) {
	return (response && response.getResponseHeader['Content-Type'] && response.getResponseHeader['Content-Type'].indexOf('json') > -1);
};


function isValidDateForValidator(date, format)
{
	var dateRegex;
	var year;
	if(format == "MM/dd/yyyy"){
		dateRegex=/^\d{2}\/\d{2}\/\d{4}$/; //Basic check for format validity
	}
	else if(format == "dd/MM/yyyy"){
		dateRegex=/^\d{2}\/\d{2}\/\d{4}$/; //Basic check for format validity
	}
	else if(format == "MM/dd/yy"){
		dateRegex=/^\d{2}\/\d{2}\/\d{2}$/; //Basic check for format validity
	}
	if (!dateRegex.test(date))
		return false;
	else {
		// check date range
		var splittedDate = date.split("/");
		var str1 = splittedDate[0];
		var str2 = splittedDate[1];
		var str3 = splittedDate[2];
		var dayobj = "";
		
		if(format == "MM/dd/yyyy"){
			dayobj = new Date(str3, str1-1, str2 );
			year = dayobj.getFullYear();
			if ( (dayobj.getMonth()+1!= str1)||
					 (dayobj.getDate()!= str2)||
					 (year != str3) )
					return false;
				else
					return true;
		}
		else if(format == "dd/MM/yyyy"){
			dayobj = new Date(str3, str2-1, str1);
			year = dayobj.getFullYear();
			if ( (dayobj.getMonth()+1!= str2)||
					 (dayobj.getDate()!= str1)||
					 (year != str3) )
					return false;
				else
					return true;
		}
		else if(format == "MM/dd/yy"){
			dayobj = new Date(str3, str1-1, str2 );
			year = dayobj.getFullYear().toString().substring(2,4);
			if ( (dayobj.getMonth()+1!= str1)||
					 (dayobj.getDate()!= str2)||
					 (year != str3) )
					return false;
				else
					return true;
		}		
		
	}
}

function getFormattedDate(dateObj){
	return YAHOO.util.Date.format(dateObj, { format: "%m/%d/%Y"});
}

//function to calculate local time
//in a different city
//given the city's UTC offset
function calcTime(city, offset) {

 // create Date object for current location
 d = new Date();
 
 // convert to msec
 // add local time zone offset 
 // get UTC time in msec
 utc = d.getTime() + (d.getTimezoneOffset() * 60000);
 
 // create new Date object for different city
 // using supplied offset
 nd = new Date(utc + (3600000*offset));
 
 // return time as a string
 return "The local time in " + city + " is " + nd.toLocaleString();

}


/**
 * Function used to mask location for current user
 * @returns
 */
var maskLocation = function(obj) {
	var selectboxId = document.getElementById(obj.id);
	var locationId = selectboxId.options[selectboxId.selectedIndex].value;
	var locationName = selectboxId.options[selectboxId.selectedIndex].lable;
	// Ignore if they haven't selected an Org
	if (locationId == -1) {
		return;
	}
	
	YAHOO.util.Connect.asyncRequest("GET", 'mask/location?locationId=' + locationId, { 
		success: function (o) {
			if (!isResponseJson(o)) {
				$('maskMsg').update("Unable to change to Location " + locationName);
				return;
			}
			
			var response = YAHOO.lang.JSON.parse(o.responseText);
			if (response && response.isSuccess == true) {
				// Org updated successfully, refresh page
				document.location.reload(true);
			}
			else {
				// Errors occured while masking org, display message from server.
				$('maskMsg').update(response.msg);
			}
		}
	});
};

function maskUserLocation(obj){
	var selectboxId = document.getElementById(obj.id);
	var locationId = selectboxId.options[selectboxId.selectedIndex].value;
	document.maskLocationForm.method="POST";
	document.maskLocationForm.action="mask/user/location";
	eleById("selectedLocationId").value = locationId;
	document.maskLocationForm.submit();
}